package com.io.util;

import com.io.model.BaseModel;
import com.io.model.RealtimeRptReq;
import com.io.model.RegisterReq;
import com.io.protocol.CommandConstants;
import com.io.protocol.LengthConstants;
import io.netty.buffer.ByteBuf;

/**
 * Created by yangyx on 2016/7/21.
 */
public class DecodeBaseModel {
    /**
     *@Author yangyx 【yang_yaxu@launchdesign.com.cn】
     *@Date  2016/7/21 13:41
     *解析一个完整的BaseModel对象
     */
    public static BaseModel decodemodel(ByteBuf buf,int packageLength){
        buf.discardReadBytes();
        /*byte[] a=new byte[buf.readableBytes()];
        buf.getBytes(buf.readableBytes()-packageLength,a);*/
        //包头和长度是固定的，所以直接根据下标跳过去
        buf.skipBytes(LengthConstants.PackageHeaderLength+ LengthConstants.PackageSizeLength);
        //TAG内容
        int tag=buf.readUnsignedShort();
        //Command
        int command=buf.readUnsignedShort();

        int leftContentLength=packageLength- LengthConstants.PackageHeaderLength- LengthConstants.PackageSizeLength- LengthConstants.TagLength
                - LengthConstants.CommandLength- LengthConstants.CrcLength;
        //由于不同的命令，传的对象也是不一样的
        BaseModel model=null;
        switch(command) {
            case CommandConstants.RegisterReq://注册
                //TODO 转成注册需要的对象
                model= RegisterReq.fromByteBuf(buf,leftContentLength);
                break;
            case CommandConstants.CHANGEPASSWORD:
                //修改密码

                break;
            case CommandConstants.RealtimeRptReq:
                //TODO 转换成实时上报的对象
                model= RealtimeRptReq.fromByteBuf(buf,leftContentLength);


                break;
            case CommandConstants.StatusRptReq:
                //TODO 抓换成状态上报的对象

                break;

            case CommandConstants.FaultRptReq:
                //TODO 抓换成故障上报的对象

                break;
            case CommandConstants.ChgTermPwdResp:
                //TODO 抓换成修改密码的对象

                break;
            default:
                System.out.println("--------没有符合条件的命令--------");
                break;
        }
        if(null != model){
            model.setTag(tag);
            model.setCommand(command);
        }
        return  model;
    }

}
